package kr.go.nih.biomedi.pSEVA.bean;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteScript {

	private String realFolder;
	private String inputFile;
	private String processName;
	private String cmd;
	private Boolean result = false;

	public String getRealFolder() {
		return realFolder;
	}

	public void setRealFolder(String realFolder) {
		this.realFolder = realFolder;
	}

	public String getInputFile() {
		return inputFile;
	}

	public void setInputFile(String inputFile) {
		this.inputFile = inputFile;
	}

	public String getProcessName() {
		return processName;
	}
	
	public void setProcessName(String processName) {
		this.processName = processName;
	}
	
	public String getCmd() {
		return cmd;
	}
	
	public void setCmd(String cmd) {
		this.cmd = cmd;
	}
	
	public String execute() {
		String strCmd=this.getCmd();
		
		try {
			String FileWriteName;
			BufferedWriter FileWriteBuffer;
			StringBuffer OutputString=new StringBuffer();
			
			OutputString.append("#!/bin/bash\n");
			OutputString.append("#$ -b y\n");
			OutputString.append("#$ -V\n");
			OutputString.append("#$ -cwd\n");
			OutputString.append("#$ -N openmpi_job_"+this.getInputFile()+"\n");
			OutputString.append("#$ -wd "+this.getRealFolder() + "\n");
			OutputString.append("#@ class = normal\n");
			OutputString.append(strCmd+"\n");
			
			FileWriteName=this.getRealFolder()+"/"+this.getInputFile()+"."+this.getProcessName()+".sh";
			FileWriteBuffer=new BufferedWriter(new FileWriter(FileWriteName));
			FileWriteBuffer.write(OutputString.toString());
			FileWriteBuffer.close();
			
		} catch (IOException ioe) {
			ioe.printStackTrace();
			result = false;
		}
		
		if (result == true) {
			return "true";
		} else {
			return "false";
		}
	}

}
